package com.eric.service.impl;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eric.domain.dto.SysPermissionDto;
import com.eric.domain.po.SysPermission;
import com.eric.mapper.SysPermissionMapper;
import com.eric.service.ISysPermissionService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author 新哥
 * @since 2024-03-12
 */
@Service
public class SysPermissionServiceImpl extends MPJBaseServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {

    @Transactional
    @Override
    public SaResult savePermissionListService(List<SysPermissionDto> permissionDtos) {
//        先判断传传进来的数据是否有Null," "等无效数据
        for (SysPermissionDto permissionDto : permissionDtos) {
            if (StrUtil.hasBlank(permissionDto.getPermissionName())){
                return SaResult.error("输入有误，请检查后重新输入！");
            }
        }
//        1、从数据库中获取到所有的权限
        List<SysPermission> listInDbs = list();
//        2、把数据库中取得的数组中的permissionName放在一个集合中List<String>ListDb
        List<String>listNameInDb=new ArrayList<>();
        for (SysPermission listInDb : listInDbs) {
            listNameInDb.add(listInDb.getPermissionName());
        }
//        3、把前段出来的permissionDtos中的permissionName放在一个集合List<String>listNameInVue
        List<String>listNameInVue=new ArrayList<>();
        for (SysPermissionDto sysPermissionDto : permissionDtos) {
            listNameInVue.add(sysPermissionDto.getPermissionName());
        }
//        4、找出ListNameInVue中有不在ListDb中的元素，放在一个List<String>中，用List的removeAll()方法
        listNameInVue.removeAll(listNameInDb);
//        5、把差值转发为SysRole对象并存进数据库
        if (listNameInVue.isEmpty()){
            return SaResult.ok("输入的角色已经存在数据库中，不用重复输入");
        }
        List<SysPermission>permissionList=new ArrayList<>();
        for (String permissionName : listNameInVue) {
            SysPermission sysPermission=new SysPermission();
            permissionList.add(sysPermission.setPermissionName(permissionName));
        }
//        6、把sysPermission批量写景数据库
        saveBatch(permissionList);
        return SaResult.ok();
    }

    @Override
    public SaResult getAllPermissions() {
        List<SysPermission>list=list();
        List<SysPermissionDto> sysPermissionDtos = BeanUtil.copyToList(list, SysPermissionDto.class);
        List<String>permissionList=new ArrayList<>();
        for (SysPermissionDto sysPermissionDto : sysPermissionDtos) {
            permissionList.add(sysPermissionDto.getPermissionName());
        }
        return SaResult.data(permissionList);
    }

    @Override
    @Transactional
    public SaResult modifyOneService(SysPermissionDto permissionDto) {
        if (BeanUtil.isEmpty(permissionDto)){
            return SaResult.error("对象不能为空！");
        }

        if (permissionDto.getId()==null){
            return SaResult.error("id参数缺失");
        }
        String permissionName = permissionDto.getPermissionName();
        String trim = StrUtil.trim(permissionName);

        if (StrUtil.isBlank(trim)){
            return SaResult.error("权限名称不能为空");
        }
        SysPermission sysPermission = BeanUtil.copyProperties(permissionDto, SysPermission.class);
        updateById(sysPermission);

        return SaResult.ok("succeed!");
    }

    @Override
    public SaResult getByNameService(String name, Integer current, Integer pageSize) {
        if (current==null){
            current=1;
        }
        if (pageSize==null){
            pageSize=10;
        }
        String nameTrim = StrUtil.trim(name);
        if (StrUtil.isBlank(nameTrim)){
            return SaResult.error("名字不能为空");
        }
        Page<SysPermission>page=new Page<>(current,pageSize);
        List<SysPermission> sysPermissionList = query()
                .like("permission_name", nameTrim)
                .orderByDesc("update_time")
                .page(page).getRecords();
        List<SysPermissionDto> sysPermissionDtoList = BeanUtil.copyToList(sysPermissionList, SysPermissionDto.class);
        return SaResult.data(sysPermissionDtoList);
    }
}
